package com.cn.wanxi.servlet.manage.nav;

import com.cn.wanxi.dto.NavModelDto;
import com.cn.wanxi.service.INavService;
import com.cn.wanxi.service.impl.NavServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/NavAdd")
public class NavAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        req.setCharacterEncoding("UTF-8");//请求编码格式为utf-8
//        resp.setCharacterEncoding("UTF-8");//响应编码格式为utf-8
//        resp.setContentType("text/json;charset=utf-8");//解决传值乱码
        //        1.接收页面数据
        String href = req.getParameter("hrefa");
        String title = req.getParameter("title");
//        2.封装页面数据
        NavModelDto navModelDto = new NavModelDto();
        navModelDto.setHref(href);
        navModelDto.setTitle(title);
//        3.调用服务逻辑层
        INavService iNavService = new NavServiceImpl();
        int num = iNavService.add(navModelDto);//num代表影响的数据条数，如果是1，则表示成功，如果是其他就是失败

//        4.返回页面
        //新增完成以后跳转到查询页面
//        req.getRequestDispatcher("back/navSelect.jsp").forward(req, resp);
//因为我们不需要传递值，所以我们直接用页面跳转
//        resp.sendRedirect("back/navSelect.jsp");
        resp.getWriter().println(num);//将导航增加结果返回给页面
    }
}
